package com.jinkeen.xiaxie.util

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.net.HttpURLConnection
import java.net.URL

/**
 * @description: TODO
 * @author whz
 * @date 2022-08-04 11:50
 * @version 1.0
 */

class BitmapUtil {
    fun imgUrlToBitmap(url: String, block: (Bitmap) -> Unit) {
        GlobalScope.launch(Dispatchers.IO) {
            val url = URL(url)
            val conn = url.openConnection() as HttpURLConnection
            conn.doInput = true
            conn.connect()
            val input = conn.inputStream
            val bitmap = BitmapFactory.decodeStream(input)
            block(bitmap)
        }
    }

    fun base64ToBitmap(base64: String): Bitmap {
        val bytes = Base64.decode(base64, Base64.DEFAULT)
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
    }
}